﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_CountryAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataWhenEdit();
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblCountryName.Text = MultipleLanguage.LBL_COUNTRY_NAME[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
        rdoDomestic.Text = MultipleLanguage.LBL_DOMESTIC[nDefaultLanguage - 1];
        rdoOversea.Text = MultipleLanguage.LBL_OVERSEA[nDefaultLanguage - 1];
        lblType.Text = MultipleLanguage.LBL_COUNTRYTYPE[nDefaultLanguage - 1];
    }
    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataWhenEdit();
    }
    #endregion

    #region Relative Functions
    private void ValidateControl()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        rfvCountryname.ErrorMessage = MessagesValidate.COUNTRY_EMPTY[nDefaultLanguage - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nCountryType = 0;
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    Country objCountry = new Country();
                    objCountry.LoadById(nID);


                    nCountryType = objCountry.Data.CountryType;
                    if (nCountryType == 1)
                    {
                        rdoDomestic.Checked = true;
                        rdoOversea.Checked = false;
                    }
                    else
                    {
                        rdoOversea.Checked = true;
                        rdoDomestic.Checked = false;
                    }

                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtCountryName.Text = ConvertData.ConvertToString(objCountry.Data.CountryName1);
                            break;
                        case 2:
                            txtCountryName.Text = ConvertData.ConvertToString(objCountry.Data.CountryName2);
                            break;
                        case 3:
                            txtCountryName.Text = ConvertData.ConvertToString(objCountry.Data.CountryName3);
                            break;
                    }
                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COUNTRY;
            Response.Redirect(sLink);
        }
    }
    private eCountry SetData()
    {
        Country objCountry = new Country();
        eCountry eCountry = this.CountryEntity();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        if (rdoDomestic.Checked) objCountry.Data.CountryType = 1;
        else objCountry.Data.CountryType = 0;
        switch (nCurrentLanguage)
        {
            case 1:
                objCountry.Data.CountryName1 = ConvertData.ConvertToString(txtCountryName.Text.Trim());

                objCountry.Data.CountryName2 = ConvertData.ConvertToString(eCountry.CountryName2);
                objCountry.Data.CountryName3 = ConvertData.ConvertToString(eCountry.CountryName3);
                break;
            case 2:
                objCountry.Data.CountryName2 = ConvertData.ConvertToString(txtCountryName.Text.Trim());

                objCountry.Data.CountryName1 = ConvertData.ConvertToString(eCountry.CountryName1);
                objCountry.Data.CountryName3 = ConvertData.ConvertToString(eCountry.CountryName3);
                break;
            case 3:
                objCountry.Data.CountryName3 = ConvertData.ConvertToString(txtCountryName.Text.Trim());

                objCountry.Data.CountryName1 = ConvertData.ConvertToString(eCountry.CountryName1);
                objCountry.Data.CountryName2 = ConvertData.ConvertToString(eCountry.CountryName2);
                break;
        }
        return objCountry.Data;
    }
    private eCountry CountryEntity()
    {
        eCountry entityCountry = new eCountry();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Country objCountry = new Country();
            objCountry.LoadById(nItemID);
            entityCountry = objCountry.Data;
        }
        return entityCountry;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        Country objCountry = new Country();

        objCountry.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objCountry.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COUNTRY + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objCountry.Update(nID);
                if (nResult > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COUNTRY);

    }
    #endregion
}